package com.wuwei.aliocr.jiwu;

import com.wuwei.aliocr.domain.Question;
import com.wuwei.aliocr.utils.HttpUtils;
import lombok.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 通过卷号获取所有题目的测试类
 */
@Component
@Setter
@Getter
public class GetTiHao {



    private   Map<String,String> header = new HashMap<>();

    private  List<String> keys = new ArrayList<>();


    @Resource
    private PoJieQuestion getQuestion;




    /**
     * 解析机务在线的数据，获取试卷编号
     */
    @SneakyThrows
    public void getTestPager(String juanHao){
        this.getHead();
        Map<String,String> query = new HashMap<>();
        query.put("a","reviews");
        query.put("q","view");
        query.put("id",juanHao);
        HttpResponse response = HttpUtils.doGet("https://www.airacm.com", "/learning.php", "GET", header, query);
        HttpEntity entity = response.getEntity();
        if (response.containsHeader(HTTP.CONTENT_ENCODING)) {
            entity = new GzipDecompressingEntity(entity);  // Gzip解压缩
        }
        if (entity != null) {
            String responseString = EntityUtils.toString(entity);
            this.pagerDeCodeHtml(responseString,juanHao);
            // 处理响应体内容
        }
    }

    /**
     * 解析点击某个试卷编号后机务在线网站上获取的内容
     * @param responseString
     */
    @SneakyThrows
    private void pagerDeCodeHtml(String responseString,String juanHao) {

        // 解析HTML文档
        Document doc = Jsoup.parse(responseString);
        // 获取所有的 <script> 标签
        Elements scripts = doc.select("script");

        // 遍历每个 <script> 标签，提取其中的 JavaScript 内容
        for (int i = 0; i < scripts.size(); i++) {
            Element script = scripts.get(i);
            String jsCode = script.html();
            if (jsCode.contains("var examview")) {
                int i1 = jsCode.indexOf("record : [");
                int i2 = jsCode.indexOf("],");
                String substring = jsCode.substring(i1 + 10, i2);
                String[] tihaos = substring.split(",");
                List<String> list1 = Arrays.asList(tihaos);
                this.getQuestion(list1,juanHao);
            }
        }
    }

    /**
     * 添加请求头，该请求头为https://www.airacm.com/learning.php?a=reviews&q=view&id=8953660该请求中的，这是在点击机务在线某个试卷编号的加载模式时
     * 此时会以加载模式加载题目的内容
     */
    @SneakyThrows
    public void getHead() {
        this.header = new HashMap<>();
        this.keys = new ArrayList<>();
        String fileName = "src\\main\\resources\\GetTiHao\\header.txt";
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        while (line!=null){
//            System.out.println(line);
            if (line.length() > 1) {
                int i = line.indexOf(":");
//                System.out.println(line.substring(0,i));
                this.keys.add(line.substring(0,i));
//                System.out.println(line.substring(i+2));
                this.header.put(line.substring(0,i),line.substring(i + 2));
            }
            line = bufferedReader.readLine();
        }
    }


    @SneakyThrows
    public void getQuestion(List<String> data,String juanHao) {
        int size = data.size();
        for (int i = 0; i <size; i+=5) {
            String records = "";
            if ((i+4)<data.size()) {
                String i1 = data.get(i);
                String i2 = data.get(i + 1);
                String i3 = data.get(i + 2);
                String i4 = data.get(i + 3);
                String i5 = data.get(i + 4);
                records = i1 +";" + i2 + ";" + i3 + ";" + i4 + ";" + i5 + ";";
            } else {
                for (int j = i; j < size ; j++) {
                    String integer = data.get(j);
                    if ("".equals(records)) {
                        records += integer;
                    } else {
                        records += ";"+integer;
                    }
                }
            }
            getQuestion.getJuWu(records,juanHao,String.valueOf(i),juanHao);
            Thread.sleep(3000);
        }
//        getQuestion.pojie();
    }
}
